package io;

import model.Player;

public final class PacketParser {
    private static final int[] PACKET_SIZES = {
        0, 3, -1
    };

    public static void parsePlayerPackets(Player p) {
        synchronized (p.in()) {
            p.in().setOffset(0);
            try {
                for (int i = 0; i < 50; i++) {
                    if (p.in().remaining() == 0) {
                        break;
                    }
                    if (!p.isVisible()) {
                        LoginProtocol.login(p);
                        break;
                    }
                    int opcode = p.in().getOpcode();
                    int size = PACKET_SIZES[opcode];
                    if (size == -1) {
                        size = p.in().getUnsignedByte();
                    }
                    switch (opcode) {
                    case 0:
                       break;
                    case 1:
                       p.findRoute(p.x() + p.in().getByte(), p.y() + p.in().getByte(), p.in().getUnsignedByte() == 1);
                       break;
                    case 2:
                       p.doChat(p.in().getString());
                       break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            p.in().setLength(0);
        }
    }
}